﻿using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.Base;


namespace Firestarter.DB.Schema.UI
{
    public class RenamedMergableBoCollectionBuilder
    {

        private DBDatabase _dbDatabase;
        public RenamedMergableBoCollectionBuilder(DBDatabase dbDatabase)
        {
            _dbDatabase = dbDatabase;
        }


        public IBusinessObjectCollection CreateBoCollection(IMergableBo selectedMergableBo)
        {

            DBTable table = selectedMergableBo as DBTable;

            if (table!=null)
            {
                return CreateDBTableBoCollection(selectedMergableBo);
            }

            throw new HabaneroDeveloperException("MergableBo was not of type DBTable or DBColumn.");
        }

        private IBusinessObjectCollection CreateDBTableBoCollection(IMergableBo selectedMergableBo)
        {
            ChangeType searchForChangeType = ChangeType.None; 
            if (selectedMergableBo.PendingChangeType==ChangeType.Add)
            {
                searchForChangeType = ChangeType.Delete;
            }
            else if (selectedMergableBo.PendingChangeType==ChangeType.Delete)
            {
                searchForChangeType = ChangeType.Add;
            }else
            {
               // return new BusinessObjectCollection<DBTable>(); ;
            }

            BusinessObjectCollection<DBTable> tablesBoCol = new BusinessObjectCollection<DBTable>();
            foreach (var table in _dbDatabase.Tables)
            {
                if (table.PendingChangeType==searchForChangeType) tablesBoCol.Add(table);
            }

            return tablesBoCol;
        }

        //private BusinessObjectCollection<DBTable> CreateRenamedDBTableCollection(DBTable table)
        //{
        //    ChangeType changeType = ChangeType.None;
        //    if (table.PendingChangeType==ChangeType.Add) changeType = ChangeType.Delete;
        //    if (table.PendingChangeType==ChangeType.Delete) changeType = ChangeType.Add;

        //    foreach (var t in _dbDatabase.Tables)
        //    {
        //        if (table.PendingChangeType==changeType) 

        //    }
        //}
    }
}
